package com.jshoperxms.controller.api.langdu;

import com.jshoperxms.Response;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.PrimaryKey;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.enums.YdEnums;
import com.jshoperxms.controller.utils.statickey.StaticKey;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.MemberT;
import com.jshoperxms.entity.YdArticleRecordRdT;
import com.jshoperxms.entity.YdDailymtaskRdT;
import com.jshoperxms.entity.YdDailytaskT;
import com.jshoperxms.service.MemberTService;
import com.jshoperxms.service.YdArticleRecordRdTService;
import com.jshoperxms.service.YdDailymtaskRdTService;
import com.jshoperxms.service.YdDailytaskTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 2019/1/8.
 * Des:
 */
@RestController
@RequestMapping("/api/dailytask")
public class DailyTaskController {

    @Resource
    private MemberTService memberTService;

    @Resource
    private YdDailymtaskRdTService ydDailymtaskRdTService;

    @Resource
    private YdArticleRecordRdTService ydArticleRecordRdTService;

    @Resource
    private YdDailytaskTService ydDailytaskTService;

    /**
     * 获取会员当月任务情况
     * @param rq
     * @return
     */
    @RequestMapping(value="/findMemberTaskPerMonth",method= RequestMethod.POST)
    public ResponseEntity findMemberTaskPerMonth(@RequestBody YdDailymtaskRdT rq){
        Map<String,Object> responsedata=new HashMap<>();
        if(StringUtils.isNotBlank(rq.getWxopenid())&&StringUtils.isNotBlank(rq.getTaskyear())&&StringUtils.isNotBlank(rq.getTaskmonth())){
            Criterion criterion=Restrictions.and(Restrictions.eq("wxopenid",rq.getWxopenid())).add(Restrictions.eq("taskyear",rq.getTaskyear())).add(Restrictions.eq("taskmonth",rq.getTaskmonth()));
            List<YdDailymtaskRdT>list=this.ydDailymtaskRdTService.findByCriteria(YdDailymtaskRdT.class,criterion);
            responsedata.put(TagWords.BEANLIST,list);
            responsedata.put(TagWords.SUCCESS,true);
        }else{
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }
        return Response.success(responsedata);
    }

    /**
     * 获取会员完成的当日任务
     * @param rq
     * @return
     */
    @RequestMapping(value="/findMemberDailyTask",method= RequestMethod.POST)
    public ResponseEntity findMemberDailyTask(@RequestBody YdDailymtaskRdT rq){
        Map<String,Object> responsedata=new HashMap<>();
        if(StringUtils.isNotBlank(rq.getWxopenid())&&StringUtils.isNotBlank(rq.getTaskdate())){
            Criterion criterion=Restrictions.and(Restrictions.eq("wxopenid",rq.getWxopenid())).add(Restrictions.eq("taskdate",rq.getTaskdate()));
            List<YdDailymtaskRdT>list=this.ydDailymtaskRdTService.findByCriteria(YdDailymtaskRdT.class,criterion);
            responsedata.put(TagWords.SUCCESS,true);
            responsedata.put("results",list);
        }else{
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }
        return Response.success(responsedata);
    }


    /**
     * 获取每日推荐的图文内容
     * @return
     */
    @RequestMapping(value="/findDailyArtRecordTask",method= RequestMethod.POST)
    public ResponseEntity findDailyRecommendArtRecord(@RequestBody YdDailytaskT rq){
        Map<String,Object> responsedata=new HashMap<>();
        if(StringUtils.isNotBlank(rq.getTaskdate())){
            Criterion criterion=Restrictions.eq("taskdate",rq.getTaskdate());
            YdDailytaskT task=this.ydDailytaskTService.findOneByCriteria(YdDailytaskT.class,criterion);
            if(task!=null){
                YdArticleRecordRdT ydArticleRecordRdT=this.ydArticleRecordRdTService.findByPK(YdArticleRecordRdT.class,task.getRpactionid());
                responsedata.put(TagWords.BEAN,ydArticleRecordRdT);
                responsedata.put(TagWords.SUCCESS,true);
            }else{
                Criterion criterion1=Restrictions.and(Restrictions.eq("recommend","1")).add(Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState()));
                YdArticleRecordRdT ydArticleRecordRdT=this.ydArticleRecordRdTService.findOneByCriteria(YdArticleRecordRdT.class,criterion1);
                responsedata.put(TagWords.BEAN,ydArticleRecordRdT);
                responsedata.put(TagWords.SUCCESS,true);
            }

        }else{
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }
        return Response.success(responsedata);

    }






    /**
     * 保存会员进行的每日任务
     * @return
     */
    @RequestMapping(value="/saveMemberDailyTask",method= RequestMethod.POST)
    public ResponseEntity saveMemberDailyTask(@RequestBody YdDailymtaskRdT rq){
        Map<String,Object> responsedata=new HashMap<>();

        if(StringUtils.isNotBlank(rq.getWxopenid())&&StringUtils.isNotBlank(rq.getTaskdate())&&StringUtils.isNotBlank(rq.getTasktype())) {
            //先查询是否当日已经添加过任务
            Criterion taskcriterion=Restrictions.and(Restrictions.eq("taskdate",rq.getTaskdate())).add(Restrictions.eq("wxopenid",rq.getWxopenid())).add(Restrictions.eq("tasktype",rq.getTasktype()));
            YdDailymtaskRdT bean=this.ydDailymtaskRdTService.findOneByCriteria(YdDailymtaskRdT.class,taskcriterion);
            if(bean!=null){
                responsedata.put(TagWords.SUCCESS,true);
                responsedata.put(TagWords.BEAN,bean);
            }else{
                Criterion criterion = Restrictions.eq("wxopenid", rq.getWxopenid());
                MemberT memberT = this.memberTService.findOneByCriteria(MemberT.class, criterion);
                if (memberT != null) {
                    bean=new YdDailymtaskRdT();
                    bean.setId(PrimaryKey.get().toString());
                    bean.setWxopenid(rq.getWxopenid());
                    bean.setStatus(BaseEnums.DataUsingState.USING.getState());
                    bean.setCreatetime(DateTools.getSystemTimeLong());
                    bean.setCreatorid("0");
                    bean.setUpdatetime(DateTools.getSystemTimeLong());
                    bean.setVersiont(1);
                    bean.setMemberid(memberT.getId());
                    bean.setTaskyear(rq.getTaskyear());
                    bean.setTaskmonth(rq.getTaskmonth());
                    bean.setTaskday(rq.getTaskday());
                    bean.setTaskdate(rq.getTaskdate());
                    bean.setTasktype(rq.getTasktype());
                    bean.setRpactionid(StaticKey.EMPTY);
                    bean.setTaskstatus(YdEnums.TASKSTATUS.UNFINISH.getState());
                    this.ydDailymtaskRdTService.save(bean);
                    responsedata.put(TagWords.SUCCESS,true);
                    responsedata.put(TagWords.BEAN,bean);
                }else{
                    responsedata.put(TagWords.SUCCESS,false);
                    responsedata.put(TagWords.MSG, WarnWrods.EMPTY_RECORD);
                }
            }
        }else{
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }
        return Response.success(responsedata);
    }
}
